// NewTrainingFramework.cpp : Defines the entry point for the console application.
//
#include "cResourceManager.h"
#include "cSceneManager.h"

#include "../Utilities/utilities.h"
#include "Vertex.h"
#include "cShader.h"
#include <conio.h>
#include <stdio.h>
#include "cCamera.h"
#include "CVideoDriver.h"

GLfloat delta = 1;
GLfloat angle1 = 3.14f/180.0f;
GLfloat angle2 = -3.14f/180.0f;


int Init ( ESContext *esContext )
{
	glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
	cResourceManager::GetInstance()->ReadResourceFile();
	cSceneManager::GetInstance()->ReadSceneFile();
	return 0;
}

void Draw ( ESContext *esContext )
{
	int index=0;
	glClear(GL_COLOR_BUFFER_BIT);
	
	CVideoDriver::GetInstance()->SetFBO(index);
	cSceneManager::GetInstance()->DrawAllObjects();
	
	CVideoDriver::GetInstance()->StopFBO();

	//CVideoDriver::GetInstance()->DrawNomal(CVideoDriver::GetInstance()->getiFBOColor());
	//CVideoDriver::GetInstance()->Drawblur(CVideoDriver::GetInstance()->getiFBOColor());
	//CVideoDriver::GetInstance()->DrawBlackWhite(CVideoDriver::GetInstance()->getiFBOColor());
	//CVideoDriver::GetInstance()->DrawBloom(CVideoDriver::GetInstance()->getiFBOColor());
	//GLuint bloompre = 0;
	GLuint *colorbuffer= CVideoDriver::GetInstance()->getiFBOColor();
//	CVideoDriver::GetInstance()->DrawBloom(colorbuffer);
//	CVideoDriver::GetInstance()->DrawBloompro(CVideoDriver::GetInstance()->getiFBOColor());
	

	eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
}

void Update ( ESContext *esContext, float deltaTime )
{
	cCamera::GetInstance()->ComputeViewMatrix();
}

void Key ( ESContext *esContext, unsigned char key, bool bIsPressed)
{
	switch(key)
	{
	case 'w': case 'W': cCamera::GetInstance()->MoveForward(delta); break;
	case 's': case 'S': cCamera::GetInstance()->MoveBack(delta); break;
	case 'a': case 'A': cCamera::GetInstance()->MoveLeft(delta); break;
	case 'd': case 'D': cCamera::GetInstance()->MoveRight(delta); break;
	case 'q': case 'Q': cCamera::GetInstance()->MoveUp(delta); break;
	case 'e': case 'E': cCamera::GetInstance()->MoveDown(delta); break;
	case '4': cCamera::GetInstance()->RotateY(angle2); break;
	case '6': cCamera::GetInstance()->RotateY(angle1); break;
	
 	}
}

void CleanUp()
{	
	cResourceManager::DestroyInstance();
	cSceneManager::DestroyInstance();
	cCamera::DestroyInstance();
	CVideoDriver::DestroyInstance();
}

int main(int argc, char* argv[])
{
	ESContext esContext;

    esInitContext ( &esContext );

	esCreateWindow ( &esContext, "Hello Triangle",  SCREEN_W,  SCREEN_H, ES_WINDOW_RGB);

	if ( Init ( &esContext ) != 0 )
		return 0;

	
	esRegisterDrawFunc ( &esContext, Draw );
	esRegisterUpdateFunc ( &esContext, Update );
	esRegisterKeyFunc ( &esContext, Key);

	esMainLoop ( &esContext );

	//releasing OpenGL resources
	CleanUp();

	//identifying memory leaks
	MemoryDump();
	printf("Press any key...\n");
	_getch();

	return 0;
}


